<template>
    <div class="container sub-container">
        <div class="inner">
            <Breadcrumb class="system-breadcrumb">
                <BreadcrumbItem to="/meeting1">三会</BreadcrumbItem>
                <BreadcrumbItem>
                    <span v-if="isEdit==0">新建</span>
                    <span v-if="isEdit==1">修改</span>
                </BreadcrumbItem>
            </Breadcrumb>
            <div class="system-form system-form-large">
                <Form ref="form" :model="form" :rules="ruleValidate" :label-width="120" :hide-required-mark="true">
                    <FormItem prop="title" label="标题">
                        <Input class="common-input" v-model="form.title" placeholder="请输入标题" />
                    </FormItem>
                    <FormItem prop="employeeIds" label="接收人">
                        <!-- {{userList}} -->
                        <Select class="common-input" v-model="form.employeeIds"  multiple  placeholder="请选择接收人">
                            <Option v-for="item in userList" :value="item.deptId" :key="item.deptId">{{ item.deptName }}</Option>
                        </Select>
                    </FormItem> 
                    <!-- <FormItem prop="exTime" label="会议时间">
                        <DatePicker v-model="form.exTime" type="datetime" ref="exTime"
                        @on-change="changeTime('exTime')"  :editable="false" placeholder="请选择会议时间"></DatePicker>
                    </FormItem> -->
                    <FormItem prop="exTime" label="会议时间">
                        <DatePicker v-model="form.exTime" type="datetime" ref="exTime" :options="pickerBeginDateAfter"
                        @on-change="changeTime('exTime')"  :editable="false" placeholder="请选择会议时间"></DatePicker>
                         <!-- <DatePicker type="date" :options="options3" placeholder="Select date" style="width: 200px"></DatePicker> -->
                    </FormItem>
                    <FormItem prop="content" label="大会内容">
                        <system-quill-editor v-model="form.content" ref='quillChild'></system-quill-editor>
                    </FormItem>
                    <FormItem>
                        <Button type="primary" class="margin-right-25" @click="handleSubmit('form')">
                            <span v-if='isEdit==1 && status!=1'>修改</span>
                            <span v-else>发送</span>
                        </Button>
                        <Button type="primary" class="margin-right-25" @click="handleDraftSubmit('form')">
                            <span v-if='isEdit!=1'>保存草稿</span>
                            <span v-if='isEdit==1'>修改草稿</span>
                        </Button>
                        <Button type="primary" @click="goBack" ghost>返回</Button>
                    </FormItem>
                </Form>
            </div>
        </div>
    </div>
</template>

<script> 
import moment from 'moment'
import SystemQuillEditor from '@/components/SystemQuillEditor'
import { getUserListNoselfRest, sendMessageRest,meetingCreate,meetingView,meetingEdit,employeeAll } from '@/common/apis/system'

export default {
    name: "addmt1",
    components: {
        SystemQuillEditor
    },
    data() {
        return {
            type: 1,
            userList: [],
            form: {
                title: '',
                employeeIds: '',
                exTime:'',
                content: '',
                status:''
            },
            ruleValidate: {
                title: [
                    { required: true, message: '标题不能为空', trigger: 'blur' }
                ],
                employeeIds: [
                    { required: true, type: 'array', message: '接收人不能为空', trigger: 'change' }
                ],
                // exTime:[
                //     { required: true,type: 'date', message: '会议时间不能为空', trigger: 'change' }
                // ],
                // content: [
                //     { required: true, message: '公告内容不能为空', trigger: 'blur' }
                // ]
            },
            isEdit:0, // 1修改，0新增
            pickerBeginDateAfter:{
                disabledDate(date){
                         return date && date.valueOf() < Date.now() - 86400000;
                    }
            },
            status:''
        }
    },
    mounted() {
        this.initData() 
        if(this.$route.query.id){
            this.getData()
            this.isEdit=1
        }else{
            this.isEdit=0
        }
    },
    methods: {
        getData(){
            let id = this.$route.query.id
            meetingView(id).then(res=>{
                    console.log("res")
                    console.log(res)
                    if(res.data.code==200){
                        // this.mtDetail = res.data.content
                        this.form.title = res.data.content.title
                        this.form.employeeIds=res.data.content.member
                        this.form.exTime=moment(res.data.content.submitTime).format('YYYY-MM-DD HH:mm:ss')
                        this.$refs.quillChild.content=res.data.content.content
                        this.status=res.data.content.status
                    }
                })
        },
        inputfun(val){
            // this.form.content = val
            console.log(this.form.content)
        }, 
        initData() {
            this.getUserListNoself()
        },
        getUserListNoself() {
            employeeAll().then(result =>{
                console.log("result:::")
                console.log(result)
                this.userList = result.data.content
            })
        },
        handleDraftSubmit(name){
            // 保存草稿
            let _this = this
            console.log(this.$refs.quillChild.content)
            this.form.content = this.$refs.quillChild.content
            this.form.status=1
            // this.form.exTime = moment(this.form.exTime).format("yyyy-MM-dd HH:mm:ss")
            this.$refs[name].validate(valid => {
                if (valid) {
                    if(_this.isEdit==1){
                        _this.form.exTime = moment(_this.form.exTime).format('YYYY-MM-DD HH:mm:ss')
                        let params = {
                            ..._this.form,
                            id:parseInt(_this.$route.query.id)
                        }
                        meetingEdit(params).then(result => {
                            console.log(result)
                            if (result.data.code == 200) {
                                console.log("成功")
                            _this.$Message.success('会议草稿修改成功');
                                _this.$router.push('/meeting1')
                                // this.handleSuccess()
                            } else {
                                _this.handleError(result.data.msg)
                            }
                        })
                    }else{
                        let params2 = {
                            ..._this.form
                        }
                        meetingCreate(params2).then(result => {
                            console.log(result)
                            if (result.data.code == 200) {
                                console.log("成功")
                            _this.$Message.success('会议草稿保存成功');
                                _this.$router.push('/meeting1')
                                // this.handleSuccess()
                            } else {
                                _this.handleError(result.data.msg)
                            }
                        })
                    }
                }
            })
        },
        handleSubmit(name) {
            let _this = this
            console.log(this.$refs.quillChild.content)
            this.form.content = this.$refs.quillChild.content
            this.form.status=2
            this.form.exTime = moment(this.form.exTime).format('YYYY-MM-DD HH:mm:ss')
            this.$refs[name].validate(valid => {
                if (valid) {
                    if(_this.isEdit==1){
                        //修改
                        let params = {
                            ..._this.form,
                            id:parseInt(_this.$route.query.id)
                        }
                        meetingEdit(params).then(result => {
                            console.log(result)
                            if (result.data.code == 200) {
                                console.log("成功")
                            _this.$Message.success('会议修改成功');
                                _this.$router.push('/meeting1')
                                // this.handleSuccess()
                            } else {
                                this.handleError(result.data.msg)
                            }
                        })
                    }else{
                        let params2 = {
                            ..._this.form
                        }
                        meetingCreate(params2).then(result => {
                            console.log(result)
                            if (result.data.code == 200) {
                                console.log("成功")
                                _this.$Message.success('会议创建成功');
                                _this.$router.push('/meeting1')
                                // this.handleSuccess()
                            } else {
                                this.handleError(result.data.msg)
                            }
                        })
                    }
                    
                }
            })
        
        },
        // handleSuccess(msg) {
        //     console.log("成公里")
        //     this.reset()
        //     this.$Message.success({
        //         background: true,
        //         content: '会议创建成功'
        //     })
        // },
        handleError(msg) {
            this.reset()
            this.$Message.error({
                background: true,
                content: msg || '会议创建失败'
            })
        },
        changeTime(refName){
            this.$refs[refName].onSelectionModeChange('time');
            this.form.exTime = this.$refs.exTime.visualValue
            console.log(this.form.exTime)
        },
        reset() {
            this.$refs.form.resetFields()
            this.$refs.myQuillEditor.handleClearEditor()
        },
        goBack() {
            this.$router.push('/meeting1')
        }
    }
}
</script>
